package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;

import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.RemindDTO;
import com.api.shopapi.service.ElderlyService;
import com.api.shopapi.service.RemindService;
import com.api.shopapi.service.StaffService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 提醒
 * */

@RequestMapping("Remind")
@RestController
public class RemindController extends BaseController {

    @Autowired
    @Qualifier("RemindServiceImplOne")
    private RemindService remindService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;


    //app查看所有消息提醒
    @RequestMapping(value = "appList", method = RequestMethod.POST)
    public MyResult getListByUserCode(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");
//            if (!checkSignPC(timeStr, sign)) {
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Remind/appList");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            PageInfo<Remind> remindPageInfo=remindService.getListByUserCode(pageIndex,pageSize,userCode);
            return success(remindPageInfo);
        } catch (Exception e) {
            insertError("/Remind/appList",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //app修改读取状态
    @RequestMapping(value = "UpdateByStatus",method = RequestMethod.POST)
    public MyResult UpdateByStatus(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
//            String userCode = jsonObject.getString("userCode");
            String remindCode =jsonObject.getString("remindCode");

//            if (!checkSignPC(timeStr, sign)) {
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Remind/UpdateByStatus");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Remind remind =remindService.getByRemindCode(remindCode);
            remind.setStatus(1);
            remindService.update(remind);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);

        } catch (Exception e) {
            insertError("/Remind/UpdateByStatus",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //提醒记录
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            String startTimeStr = jsonObject.getString("startTime");
            String endTimeStr = jsonObject.getString("endTime");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime = sdf.parse(startTimeStr);
            Date endTime = sdf.parse(endTimeStr);

            Calendar calendar = new GregorianCalendar();
            calendar.setTime(endTime);
            calendar.add(calendar.DATE, 1); //把日期往后增加一天,整数  往后推,负数往前移动
            endTime = calendar.getTime(); //这个时间就是日期往后推一天的结果


            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Remind/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Staff staff =staffService.getByStaffCode(staffCode);
            String governmentCode=staff.getGovernmentCode();
            PageInfo<RemindDTO> remindDTOPageInfo=remindService.getPageList(pageIndex,pageSize,governmentCode,startTime,endTime);
            List<RemindDTO> remindDTOList =remindDTOPageInfo.getList();
            for (RemindDTO remindDTO:remindDTOList){
                Elderly elderly =elderlyService.getByUserCode(remindDTO.getUserCode());
                remindDTO.setElderly(elderly);
            }
            return success(remindDTOPageInfo);
        } catch (Exception e) {
            insertError("/Remind/Page",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

}
